var express = require('express');
var router = express.Router();
var sqlite3 = require('sqlite3').verbose();
var path = require('path');
/* GET users listing. */
router.get('/', function(req, res, next) {
    var page = req.query.page;
    var limit = req.query.limit;
    var aname = req.query.aname;
    var db=new sqlite3.Database(path.join(__dirname, "../db/LH.s3db"));
    try{
        sqlStr = "select * from(SELECT \
            s_activity.[s_id],\
            s_name , \
            a_name , \
            class,\
            score,\
            score as lh\
          FROM \
            (s_activity JOIN \
            student on s_activity.[s_id] = student.[s_id]) join activity on s_activity.[a_no] = activity.[a_no]) where a_name= ? and score!='-1'";
            sqlStrfinal=sqlStr+"limit ?,?";
            // var aname=localStorage.getItem('aname');
            console.log(aname);
            var para = [aname,(page-1)*limit,limit*1.0];
            db.all(sqlStrfinal,para,function(err,rows){
                if(!rows){
                    table = {"code":0,"msg":"","count":0,"data":""};
                }else{
                    console.log(rows);
                    table = {"code":0,"msg":"","count":rows.length,"data":""};
                    for(var i=0;i<rows.length;i++){
                        if(parseInt(rows[i].lh)>1)
                            rows[i].lh='是';
                        else
                            rows[i].lh='否';
                    }
                    table.data=rows;
                }
                res.status(200).json(table);
            } );
    }catch(ex){
        console.log(ex);
    }
});

module.exports = router;
